'****************************************************************
'*  Name    : UNTITLED.BAS                                      *
'*  Author  : [select VIEW...EDITOR OPTIONS]                    *
'*  Notice  : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 9/3/2013                                          *
'*  Version : 1.0                                               *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************
    
    wsave var	byte $70 system
	ssave var	byte bank0 system
	psave var	byte bank0 system
	
    
    define INTHAND tic
    
    symbol    TIMER0_FLAG =  INTCON.2
    symbol    TIMER1_FLAG =  PIR1.0
    symbol    TIMER2_FLAG =  PIR1.1
    symbol  TRUE = 1
    symbol  FALSE = 0
     
    CORE_TIMER var word

    GOTO MAIN
    
INIT_TIMER0:
    OPTION_REG = %10000111     'Prep Timer0 for Counting and Self-Shutoff
    TMR0 = 0
    TIMER0_FLAG = FALSE
    RETURN
    
INIT_TIMER1:
    T1CON = 0            ' TMR1 prescale=1, clock=Fosc/4, TMR1=off
    TMR1H = 0            ' Clear high byte of TMR1 counter
    TMR1L = 0            ' Clear low byte
    TIMER1_FLAG  = FALSE
    T1CON.0 = 1          ' Turn TMR1 on here
    return
    
INIT_TIMER2:
    CORE_TIMER = 0
    TIMER2_FLAG = FALSE  ' Start Core Timer 1ms interval
    PR2 = 249
    T2CON = %00011100
    Return
    
INIT_CORE_TIMER:
    CORE_TIMER = 0
    TIMER2_FLAG = FALSE  ' Start Core Timer 1ms interval
    PR2 = 249
    T2CON = %00011100
    PIE1.1 = TRUE
    INTCON = %11000000           ' Interrupts on
    Return

;INTERUPT SERVICE ROUTINE FOR CORE TIMER
asm
	; Save W, STATUS and PCLATH registers
tic 	movwf	wsave
		swapf	STATUS, W
		clrf	STATUS
		movwf	ssave
		movf	PCLATH, W
		movwf	psave

	; Insert interrupt code here
	; Save and restore FSR if used

        ;incf  _tic, F
        incfsz  _CORE_TIMER, F
        goto exit
        incf  _CORE_TIMER+1, F
exit
        bcf   PIR1, 1
	; Restore PCLATH, STATUS and W registers
		movf	psave, W
		movwf	PCLATH
		swapf	ssave, W
		movwf	STATUS
		swapf	wsave, F
		swapf	wsave, W
		retfie
	endasm
    
    
